package com.example.user.biz.service.impl;

import com.example.common.constant.MessageConstant;
import com.example.common.context.BaseContext;
import com.example.common.exception.UpdatePasswordException;
import com.example.user.biz.domain.dto.UpdatePasswordDTO;
import com.example.user.biz.domain.dto.UpdateUserInfoDTO;
import com.example.user.biz.domain.po.User;
import com.example.user.domain.vo.UserInfoVO;
import com.example.user.biz.mapper.UserMapper;
import com.example.user.biz.service.UserService;
import com.example.user.domain.vo.VolunteerVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    /**
     * 获取用户信息
     * @return
     */
    @Override
    public UserInfoVO getUserInfo() {
        Long userId = BaseContext.getCurrentId();
        UserInfoVO userInfoVO = userMapper.selectUserInfo(userId);
        userInfoVO.setPhone(userInfoVO.getPhone().substring(0,3)+"****"+userInfoVO.getPhone().substring(7));
        return userInfoVO;
    }

    @Override
    public void updateUserInfo(UpdateUserInfoDTO updateUserInfo) {
        User user = User.builder()
                .id(BaseContext.getCurrentId())
                .gender(updateUserInfo.getGender())
                .nickname(updateUserInfo.getNickname())
                .avatar(updateUserInfo.getAvatar())
                .birthday(updateUserInfo.getBirthday())
                .region(updateUserInfo.getRegion())
                .build();
        userMapper.updateUserInfo(user);
    }

    @Override
    public VolunteerVO getVolunteer(Long userId) {
        VolunteerVO volunteerVO=userMapper.getVolunteerList(userId);
        return volunteerVO;
    }

    /**
     * 修改密码
     * @param updatePasswordDTO
     */
    @Override
    public void updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        String oldPwd = updatePasswordDTO.getOldPassword();
        String newPwd = updatePasswordDTO.getNewPassword();
        String rePwd = updatePasswordDTO.getConfirmPassword();
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);
        if(!newPwd.equals(rePwd)){
            throw new UpdatePasswordException(MessageConstant.TWO_PASSWORD_NOT_CONSISTENT);
        }

    }
}
